#!/bin/bash
# 获取当前系统版本，并判断是否为centos7
function getVersion(){
  echo "+-----------------------------------------+"
  echo "开始获取系统版本"
  if [ ! -f /etc/redhat-release ];then
	echo "此系统与脚本不兼容，脚本自动退出   [ FALSE ]"
	exit 1
  fi
  name=$(cat /etc/os-release  | grep -iw name | awk -F \" '{print $2}')
  version=$(cat /etc/os-release   | grep version_id -i | awk -F \" '{print $2}')
  if [ $version -ne 7 ];then
  	echo "此系统与脚本不兼容，脚本自动退出   [ FALSE ]"
        exit 2
  else 
  	echo "系统版本为：$name $version        [ OK ]"
	echo "+-----------------------------------------+"
  fi
}

# 检查 ISO 是否存在
function getISO(){
  echo "开始检查系统是否存在ISO块设备"
  blkid | grep /dev/sr0 -o &> /dev/null
  if [ $? -ne 0 ] ;then
	echo "ISO块设备不存在，脚本退出    [ FALSE ]"
        exit 3
  else
	echo "$(blkid | grep /dev/sr0 -o) 存在                     [ OK ]"
        echo "+-----------------------------------------+"
  fi
}

# 检查网络
function getNetwork(){
  echo "开始检查网络"
  echo "正在安装net-tools"
  yum install net-tools -y &> /dev/null
  ip=$(ifconfig | grep -A3 ens | grep cast | awk '{print $2}')
  netmask=$(ifconfig | grep -A3 ens | grep cast | awk '{print $4}')
  ping -c1 -w1 bing.com &> /dev/null
  if [ $? -eq 0 ];then
        echo "IP：$ip"
   	echo "NETMASK: $netmask"
  	echo "网络正常                          [ OK ]"
  else
	echo "网络不可达，请检查网络连通性   [ FALSE ]"
        exit 4
  fi
  echo "+-----------------------------------------+"
}

# 配置yum源
function make_yum() {
  echo "开始配置YUM仓库"
  mount /dev/sr0 /mnt &> /dev/null
  mkdir /etc/yum.repos.d/bak &> /dev/null && mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/ &> /dev/null
cat > /etc/yum.repos.d/dvd.repo <<EOF
[development]
name=redhat
baseurl=file:///mnt
gpgcheck=0
enabled=1
EOF

  if ls /etc/yum.repos.d/dvd.repo &> /dev/null;then
  	echo "YUM仓库配置成功                   [ OK ]"
  else 
        echo "YUM仓库配置失败    [ false ]"
        exit 5
  fi
  echo "+-----------------------------------------+"
}

# 关闭防火墙和SElinux
function close_fire_se() {
  echo "正在关闭防火墙"
  systemctl disable --now firewalld &> /dev/null
  echo "正在关闭SElinux"
  sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  setenforce 0
  echo "设置成功                          [ OK ]"
  echo "+-----------------------------------------+"
}

# 配置DHCP
function make_dhcp() {
  echo 开始配置DHCP
  echo "获取网络信息中"
  IP=$(ifconfig | grep -A3 ens | grep cast | awk '{print $2}')
  NETMASK=$(ifconfig | grep -A3 ens | grep cast | awk '{print $4}')
  GATEWAY=$(route -n | grep ens | awk 'NR==1{print $2}')
  IP_CIDR=$(route -n  | grep ens | awk 'NR==2{print $1}')
  echo "IP：$IP"
  echo "NETMASK：$NETMASK"
  echo "IP_CIDR：$IP_CIDR"
  echo "正在安装DHCP"
  yum install dhcp -y &> /dev/null
  IP_START=$(route -n  | grep ens | awk 'NR==2{print $1}' | awk -F . '{print $1"."$2"."$3"."20}')
  IP_END=$(route -n  | grep ens | awk 'NR==2{print $1}' | awk -F . '{print $1"."$2"."$3"."40}')
  echo "开始配置 -> /etc/dhcp/dhcpd.conf"
cat > /etc/dhcp/dhcpd.conf << EOF
subnet $IP_CIDR netmask $NETMASK {
  range $IP_START $IP_END;
  option routers $GATEWAY;
  default-lease-time 21600;
  max-lease-time 43200;
  next-server $IP;
  filename "/pxelinux.0";
}
EOF
  systemctl enable --now dhcpd
  echo "设置自启动                        [ OK ]"
  if systemctl status dhcpd | grep active &> /dev/null;then
  	echo "DHCP配置完成                      [ OK ]"
  else
	echo "DHCP配置出错     [ FALSE ]"
  fi
  echo "+-----------------------------------------+"
}

# 配置 TFTP
function make_tftp() {
  echo "开始配置TFTP"
  echo "正在安装tftp-server/xinetd"
  yum install tftp-server xinetd -y &> /dev/null
  echo "正在修改配置文件 ——> /etc/xinetd.d/tftp"
  sed -i.bak '14s/yes/no/g' /etc/xinetd.d/tftp
  echo "配置完成                          [ OK ]"
  systemctl enable --now xinetd
  echo "设置自启动                        [ OK ]"
  echo "TFTP配置完成                      [ OK ]"
  echo "+-----------------------------------------+"
}

# 安装syslinux
function make_syslinux() {
  echo "开始配置pxelinux.0"
  echo "正在安装syslinux"
  yum install syslinux -y &> /dev/null
  echo "开始拷贝文件"
  echo "/usr/share/syslinux/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0"
  echo "/mnt/isolinux/* -> /var/lib/tftpboot/"
  cp /usr/share/syslinux/pxelinux.0  /var/lib/tftpboot/
  cp /mnt/isolinux/* /var/lib/tftpboot/
  if ls /var/lib/tftpboot/pxelinux.0 &> /dev/null;then
 	echo "拷贝成功                          [ OK ]"
  else
	echo "拷贝失败    [ FALSE]" 
        exit 6
  fi
  echo "syslinux配置成功                  [ OK ]"
  echo "+-----------------------------------------+"
}

# 配置 HTTPD 
function make_http() {
  echo "开始配置HTTPD"
  echo "正在安装 httpd"
  yum install httpd -y &> /dev/null
  echo "安装成功                          [ OK ]"
  systemctl enable --now httpd &> /dev/null
  echo "自启动设置成功                    [ OK ]"
  echo "开始配置文件"
  mkdir -p /var/www/html/centos
  mount /dev/sr0 /var/www/html/centos &> /dev/null
  if ls -d /var/www/html/centos/isolinux &> /dev/null;then 
  	echo "HTTPD配置成功                     [ OK ]"
  else
	echo "配置出错    [ FALSE]"
	exit 7 
  echo "+-----------------------------------------+"
  fi
}

# 配置 kickstart
function make_kickstart() {
  echo "开始配置kickstart"
  mkdir /var/www/html/ks -p
  cp ./ks.cfg /var/www/html/ks/
  echo "kickstart配置成功                 [ OK ]"
  echo "+-----------------------------------------+"
}

# 配置 default 文件
function make_default() {
  IP=$(ifconfig | grep -A3 ens | grep cast | awk '{print $2}')
  echo "开始配置defautl" 
  mkdir /var/lib/tftpboot/pxelinux.cfg -p
  cp ./default /var/lib/tftpboot/pxelinux.cfg/
  echo "default文件拷贝成功"
  echo "正在将文件IP更换为本机IP"
  sed -i.bak "s@1.1.1.1@${IP}@g" /var/lib/tftpboot/pxelinux.cfg/default
  echo "default文件配置成功               [ OK ]"
  echo "+-----------------------------------------+"
}


getVersion
getISO
getNetwork
make_yum
close_fire_se
make_dhcp
make_tftp
make_syslinux
make_http
make_kickstart
make_default
echo -e "\033[32;1m <- PXE-KickStart 无人值守脚本配置完成！->\033[0m"
echo -e "\n"
